<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <acc-set-card></acc-set-card>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div slot="header" class="clearfix">
                <!-- <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>-->

                <!-- 搜索表单 -->
                <el-form :model="query" ref="searchRuleForm" label-width="100px">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="10" class="flex" style="justify-content:flex-start">
                            <el-form-item label="会计期间" prop="beginAccPperiod">
                                <el-select
                                    v-model="query.beginAccPperiod"
                                    placeholder="请选择"
                                    clearable
                                    @focus="getPeriodList"
                                >
                                    <el-option
                                        :label="item"
                                        :value="item"
                                        v-for="(item, index) in periodList"
                                        :key="index"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="至" prop="overAccPperiod">
                                <el-select
                                    v-model="query.overAccPperiod"
                                    placeholder="请选择"
                                    clearable
                                    @focus="getPeriodList"
                                >
                                    <el-option
                                        :label="item"
                                        :value="item"
                                        v-for="(item, index) in periodList"
                                        :key="index"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        
                        <el-col :sm="22" :md="5">
                            <el-form-item prop="haveChild">
                                <el-checkbox v-model="query.haveChild">所有子科目</el-checkbox>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="借贷方向:" prop="drFlag">
                                <el-select v-model="query.drFlag" placeholder="请选择">
                                    <el-option label="借" value="1"></el-option>
                                    <el-option label="贷" value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="22" :md="5">
                            <el-form-item label="科目" prop="subCode">
                                <div @click="showTreeDialog('科目')">
                                    <el-input v-model="subCode" readonly>
                                        <i slot="suffix" class="el-icon-search"></i>
                                    </el-input>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="22" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchRuleForm')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchRuleForm')">重 置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <vxe-table :data="tableData" border ref="multipleTable" :height="containerHeight - 200">
                <vxe-table-column field="id" title="序号" type="index" align="center"></vxe-table-column>
                <vxe-table-column field="subCode" title="科目编码"></vxe-table-column>
                <vxe-table-column field="subName" title="科目名称"></vxe-table-column>

                <vxe-table-column field="period" title="会计期间"></vxe-table-column>
                <vxe-table-column field="drFlag" title="借贷方向">
                    <template slot-scope="scope">{{scope.row.drFlag == 1?'借':'贷'}}</template>
                </vxe-table-column>
                <vxe-table-column field="beginAmt" title="本币期初金额"></vxe-table-column>
                <vxe-table-column field="debitAmt" title="本币借方金额"></vxe-table-column>
                <vxe-table-column field="crebitAmt" title="本币贷方金额"></vxe-table-column>
                <vxe-table-column field="endAmt" title="本币期末金额"></vxe-table-column>
                <vxe-table-column title="明细">
                    <template slot-scope="scope">
                        <div class @click="openDetail(scope.row.glId)">明细</div>
                    </template>
                </vxe-table-column>
            </vxe-table>
            <!-- <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="pagination.pageIndex"
                    :page-size="pagination.pageSize"
                    :total="pagination.pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div> -->
        </div>
        <tree-search-subject
            :isShow="showSearch"
            :dialogTitle="searchDialogTit"
            @edit="searchBack"
            @close="closeTreeDialog"
        ></tree-search-subject>
        <!-- <accountBookDetail 
            :id="detailId"
        />-->
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                beginAccPperiod: '', // 开始的会计期间
                drFlag: 0, // 借贷方向,0全部,1、借 2、贷',
                haveChild: 0, // 是否包含子科目,0不包含,1包含
                overAccPperiod: '', // 结束的会计期间
                subId: '' // 科目
            },
            subCode: '',

            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            tableHeader: [
                // 表头数据
                { prop: 'itemName', label: '字典项名称', minWidth: '100px', sortable: 'custom' }
            ],
            tableData: [],
            editData: {}, // 要修改的数据
            isEdit: false,
            multipleSelection: [],

            searchDialogTit: '',
            showSearch: false,
            detailId: '', // 明细Id

            periodList: []
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 200;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        // 选择
        showTreeDialog(tit) {
            console.log('showTreeDialog');
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        // 树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.subId = data.backData.id;
            this.subCode = data.backData.subCode;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                    this.findAllPage();
                })
                .catch(() => {});
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        deleteById(id) {
            appMain
                .dictItemDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 查询
        findAllPage() {
            this.loading = true;
            let sendData = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };
            sendData.record.drFlag = Number(this.query.drFlag);
            sendData.record.haveChild = this.query.haveChild ? 1 : 0;
            appMain
                .generalLedgerList(sendData)
                .then(res => {
                    this.timer = setTimeout(() => {
                        this.loading = false;
                    }, 1000);
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.tableData = res.data.data;
                        // this.pagination.pageSize = res.data.data.pageSize;
                        // this.pagination.pageIndex = res.data.data.pageNum;
                        // this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                    this.timer = setTimeout(() => {
                        //设置延迟执行
                        this.loading = false;
                    }, 1000);
                });
        },
        //分页导航

        handlePageChange({ currentPage, pageSize }) {
            this.pagination.pageIndex = currentPage;
            this.pagination.pageSize = pageSize;
            this.findAllPage();
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        getPeriodList() {
            let d = new Date();
            let yearMonth = d.getFullYear() + '' + appMain.formatZero(d.getMonth());
            this.query.beginAccPperiod = yearMonth;
            this.query.overAccPperiod = yearMonth;

            appMain
                .accPeriodFindDefaultData()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.periodList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
         // 获取当前会计期间
        accPeriodDefaultData () {
            let d = new Date();
            let date_ = d.getFullYear() + '-' + appMain.formatZero(d.getMonth() + 1) + '-' + appMain.formatZero(d.getDate());
            appMain
                 .accPeriodDefaultData({}, date_ )
                .then(res => {
                    if (res.data.code === 20000) {
                        this.query.beginAccPperiod = res.data.data;
                        this.query.overAccPperiod = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 打开明细弹窗
        openDetail(glId) {
            this.$router.push({
                path: '/accountBookDetail',
                query: {
                    id: glId
                }
            });
        }
    },
    created() {
        this.findAllPage();
        this.getPeriodList();
        this.accPeriodDefaultData()
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
.flex-left ul {
    margin-top: 20px;
}

.flex-left li {
    box-shadow: 0px 0px 4px 2px #ddd;
    height: 30px;
    line-height: 30px;
    padding-left: 20px;
    margin-bottom: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.flex-left li.current {
    box-shadow: 0px 0px 4px 2px #409eff;
}
.flex-right {
    padding: 20px;
}
</style>